#include "net.h"

//客户端
void setting(char* s)
{
	printf("\n%s ser_ip an ser_port",s);
	printf("\n\t ser_ip: server ip address");
	printf("\n\t ser_port: server_potr>5000\n");
}



int main(int argc, char* argv[])
{

	int fd = -1;
	int port;
	struct sockaddr_in soc;
	
	//创建套接字
	if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("socket");
		exit(1);
	}

	//判度启动服务器参数是否满足要求
	if(argc != 3)
	{
		setting(argv[0]);
		exit(1);
	}

	//检查输入的端口号
	port = atoi(argv[2]);
	if(port < 5000 )
	{
		perror("atoi");
		exit(1);
	}


	//填充服务器网络信息
	soc.sin_family = AF_INET;
	soc.sin_port = htons(POTR);
	if(inet_pton(AF_INET, argv[1], (void* )&soc.sin_addr) != 1)
	{
		perror("inet_pton");
		exit(1);
	}

	//与服务器连接
	socklen_t len = sizeof(soc);
	if(connect(fd, (struct sockaddr*)&soc, len) < 0)
	{
		perror("connect");
		exit(1);
	}

	//读写
	int ret = -1;
	char buf[BUFSI];
	while(1)
	{
		bzero(buf,BUFSI);
		if(fgets(buf, BUFSI-1, stdin) == NULL)
		{
			continue;

		}
		do
		{
			ret = write(fd, buf, strlen(buf));
		}while(ret < 0 && errno == EINTR);

		if (!strncasecmp (buf, QUIT, strlen (QUIT))) {	//用户输入了quit字符
			printf ("Client is exiting!\n");
			break;
		}
	}


	close(fd);
}























